<?php
/**
 * 小程序-首页
 *
 * User: kaikaic
 * Date: 2018/12/5
 * Time: 16:40
 */

namespace app\api\controller;

use app\api\service\ConsultantService;
use app\api\service\MainService;
use app\psy\service\AccountService;
use app\psy\service\ApplyService;
use controller\BasicApi;
use service\ConfigService;

class Index extends BasicApi {
    private $adSpotID = 1;

    public function index() {
        $param['recommend'] = 1;
        $param['status'] = 2;
        $limit = "0,3";
        if(cache('indexres')){

          $res =  cache('res');
        }else{
            $res['adv'] = MainService::loadAD($this->adSpotID);
            $consult= ConsultantService::getInstance()->getList($param,$limit);
            foreach ($consult as $key=>&$item){
                $item['create_time'] = date('Y-m-d',$item['create_time']);
            }
            $res['consult']  = $consult;
            $res['articel'] = MainService::getArticleList($param,$limit);
            $res['test'] = MainService::getTestList($param,$limit);
            $res['class'] = MainService::getClassList($param,'0,6');

            cache('res',$res,3600);
        }

        $this->success('获取成功',$res);
    }
    public function loadconsultadv(){
        $adv = MainService::loadAD(10);
        if($adv){
            $this->success('获取成功',$adv);
        }else{
            $this->error('获取轮播图失败',401);
        }
    }
    public function loadinsadv(){
        $adv = MainService::loadAD(11);
        if($adv){
            $this->success('获取成功',$adv);
        }else{
            $this->error('获取轮播图失败',401);
        }
    }
    public function articelinfo(){
        $id = intval($this->request->param("id"));
        $res = MainService::getArticle($id);
        if($res){
            $res['create_time'] = date('Y-m-d', $res['create_time']);
            $this->success('获取成功',$res);
        }else{
            $this->error('获取失败',401);
        }
    }
    public function articelist(){
        $type = intval($this->request->param("type"));
        if($type){
            $res = MainService::getArticleType($type);
        }else{
            $param = array();
            $pageSize = 10;
//            $param['recommend'] = 1;
            $page = intval($this->request->param('page',1));
            $start = ($page-1)*$pageSize;
            $limit = "$start,$pageSize";
            $res= MainService::getArticleList($param,$limit);
        }

        if($res){
            $this->success('获取成功',$res);
        }else{
            $this->error('获取失败',401);
        }
    }

    public function classinfo(){
        $id = intval($this->request->param("id"));
        $res = MainService::getClassInfo($id);
        if($res){
            $this->success('获取成功',$res);
        }else{
            $this->error('获取失败',401);
        }
    }
    public function testinfo(){
        $id = intval($this->request->param("id"));
        $res = MainService::getTestInfo($id);
        if($res){
            $this->success('获取成功',$res);
        }else{
            $this->error('获取失败',401);
        }
    }
    public function getclasslist(){
        $pageSize = 10;
        $param['recommend'] = 1;
        $page = intval($this->request->param('page',1));
        $start = ($page-1)*$pageSize;
        $limit = "$start,$pageSize";
        $res= MainService::getClassList($param,$limit);
        if($res){
            $this->success('获取成功',$res);
        }else{
            $this->error('获取失败',401);
        }
    }
    public function gettestlist(){
        $pageSize = 10;
        $param['status'] = 2;
        $page = intval($this->request->param('page',1));
        $start = ($page-1)*$pageSize;
        $limit = "$start,$pageSize";
        $res = MainService::getTestList($param,$limit);
        if($res){
            $this->success('获取成功',$res);
        }else{
            $this->error('获取失败',401);
        }
    }
}

